# syntax=docker/dockerfile:1.4
# vim: ft=dockerfile

FROM {{ base_image }}:{{ base_image_tag }}

# This list needs to be trimmed back later
{% if disable_cache_mounts %}
RUN \
{% else %}
RUN --mount=type=cache,target=/var/cache/apt \
{% endif %}
    apt-get update && \
    apt-get -y install --no-install-recommends \
    {% for dependency in apt_dependencies -%}
        {% if not loop.last %}
        {{ dependency }} \
        {% else %}
        {{ dependency }}
        {% endif %}
    {% endfor %}

# We need this to ensure that the wheels can be built.
# Otherwise we get "invalid command 'bdist_wheel'"
{% if disable_cache_mounts %}
RUN \
{% else %}
RUN --mount=type=cache,target=/root/.cache/pip \
{% endif %}
    pip3 install --upgrade pip --break-system-packages && \
    pip3 install wheel --break-system-packages

# Install Python requirements
COPY requirements/requirements.viewer.txt /tmp/requirements.txt

{% if disable_cache_mounts %}
RUN \
{% else %}
RUN --mount=type=cache,target=/root/.cache/pip \
{% endif %}
    pip3 install -r /tmp/requirements.txt --break-system-packages

# Works around issue with `curl`
# https://github.com/balena-io-library/base-images/issues/562
RUN c_rehash

# QT Base from packages does not support eglfs

{% if board != 'pi5' and board != 'x86' %}
RUN curl "{{webview_base_url}}/qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz" \
        -sL -o "/tmp/qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz" && \
    curl "{{webview_base_url}}/qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz.sha256" \
        -sL -o "/tmp/qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz.sha256" && \
    cd /tmp && \
    sha256sum -c "qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz.sha256" && \
    tar -xzf "/tmp/qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz" -C /usr/local && \
    rm "qt{{qt_major_version}}-{{qt_version}}-{{debian_version}}-{{board}}.tar.gz"
{% endif %}

RUN curl "{{webview_base_url}}/webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz" \
        -sL -o "/tmp/webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz" && \
    curl "{{webview_base_url}}/webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz.sha256" \
        -sL -o "/tmp/webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz.sha256" && \
    cd /tmp && \
    sha256sum -c "webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz.sha256" && \
    tar -xzf "/tmp/webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz" -C /usr/local && \
    rm "webview-{{qt_version}}-{{debian_version}}-{{board}}-{{webview_git_hash}}.tar.gz"

ENV QT_QPA_EGLFS_FORCE888=1
ENV QT_QPA_PLATFORM=linuxfb

# Turn on debug logging for now
#ENV QT_LOGGING_RULES=qt.qpa.*=true
ENV QT_LOGGING_RULES=*.debug=true
ENV QT_QPA_DEBUG=1

ENV GIT_HASH={{ git_hash }}
ENV GIT_SHORT_HASH={{ git_short_hash }}
ENV GIT_BRANCH={{ git_branch }}
ENV DEVICE_TYPE={{ board }}
ENV DJANGO_SETTINGS_MODULE="anthias_django.settings"

RUN useradd -g video viewer

RUN rm -f /etc/localtime

WORKDIR /usr/src/app
RUN mkdir -p /usr/src/app
COPY . /usr/src/app/

CMD ["bash", "./bin/start_viewer.sh"]
